package com.moxi.controller;

import com.moxi.model.User;
import com.moxi.service.UserService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {

    @Autowired
    private  UserService service;
    @RequestMapping("/admin/userManage_{pageCurrent}_{pageSize}_{pageCount}")
    public String getAllUser(User user, Model model, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount){
        //判断
        if(pageSize == 0) pageSize = 10;
        if(pageCurrent == 0) pageCurrent = 1;
        int rows = service.count(user);
        if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;

        //查询
        user.setStart((pageCurrent - 1)*pageSize);
        user.setEnd(pageSize);
        List<User> list=service.findAllUserLikeByNameAndByPage(user);
        model.addAttribute("userList",list);
        return "user/userManage";
    }

    @RequestMapping("/admin/userManageDelete")
    public String deleteUser(User user){
        service.deleteUser(user);
        return "redirect:dashboard";
    }

    /**
     * 修改用户跳转
     * @param model
     * @param user
     * @return
     */
    @GetMapping("/admin/userEdit")
    public String toUpdate(Model model,User user){
        model.addAttribute("user",service.selectById(user));
        return "user/userEdit";
    }

    /**
     * 添加用户跳转
     * @return
     */
    @GetMapping("/admin/userInsert")
    public String touserInsert(){
        return "user/userInsert";
    }

    /**
     * 添加、修改用户提交
     * @param user
     * @return
     */
    @PostMapping("/admin/userEdit")
    public String insertUser(User user, @RequestParam MultipartFile[] headPicture, HttpSession httpSession){

//        for (MultipartFile file : imageFile) {
//            if (file.isEmpty()) {
//                System.out.println("文件未上传");
//            } else {
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//                Random random = new Random();
//                java.util.Date date = new java.util.Date();
//                String strDate = sdf.format(date);
//                String fileName = strDate + "_" + random.nextInt(1000) + file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."),file.getOriginalFilename().length());
//                String realPath = httpSession.getServletContext().getRealPath("/userfiles");
//                System.out.println("realPath : "+realPath);
//                try {
//                    FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath, fileName));
//                    news.setImage("/userfiles/"+fileName);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }


        if(user.getId()!=0){
            user.setUpdateDate(new Date(new java.util.Date().getTime()));
            service.updateUser(user);
        } else {
            user.setAddDate(new Date(new java.util.Date().getTime()));
            service.insertUser(user);
        }

        return "redirect:/admin/userManage_0_0_0";

    }

}